//
//  CountryViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/14.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "CountryViewController.h"
#import "SDCycleScrollView.h"
#import "CountryModel.h"
#import "DiscountCell.h"
#import "HotCollectCell.h"
#import "AllHotCountryViewController.h"
#import "AllDiscountViewController.h"
#import "CityViewController.h"
#import "DisCountWebView.h"
#import "SuperWebViewViewController.h"
#import "MapViewController.h"

@interface CountryViewController ()<UITableViewDataSource,UITableViewDelegate,HotCollectCellDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property (nonatomic, strong) SDCycleScrollView *picView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *picArr;
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) NSDictionary *countryDic;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UICollectionView *hotCityCollect;
@property (nonatomic, strong) NSMutableArray *newsArr;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) UILabel *cnLabel;
@property (nonatomic, strong) UILabel *enLabel;
@property (nonatomic, strong) UIImageView *lineImage;
@property (nonatomic, strong) UILabel *sectLabel;
@property (nonatomic, strong) UIButton *mapButton;

@end

@implementation CountryViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
   
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@&lat=38.88268760865181&lon=121.5397062723905&page=1&track_app_channel=App%%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&v=1%%20",self.countryId];
   
    [ZSMAFNetWork geturl:url body:nil result:ZSMJSON headerFile:nil success:^(id result) {
       
     
        //超值自由行
     
        NSArray *disArr = result[@"data"][@"new_discount"];
        //热门城市
        NSArray *arrCity = result[@"data"][@"hot_city"];
        //轮播图
        self.countryDic =  result[@"data"];
        CountryModel *countrylist = [[CountryModel alloc]initWithDic:self.countryDic];
 
     
        // 轮播图图片
        self.picArr = [NSMutableArray array];
        self.picArr = countrylist.photos ;
           NSLog(@"%@",self.picArr);
       [self creatTableView];
        
        
        // 头部标题
        self.topLabel.text = countrylist.entryCont;
        self.cnLabel.text = countrylist.cnname;
        self.enLabel.text = countrylist.enname;
        self.newsArr = [NSMutableArray array];
        //超值自由行
        for (NSDictionary *dic in disArr) {
            CountryModel *dislist = [[CountryModel alloc]initWithDic:dic];
            [self.newsArr addObject:dislist];
        }
        //热门城市
        self.cityArr = [NSMutableArray array];
        for (NSDictionary *dic in arrCity) {
            CountryModel *model = [[CountryModel alloc]initWithDic:dic];
            [self.cityArr addObject:model];
        }
        [self.tableView reloadData];
         self.hud.hidden = YES;
      
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}





- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud hide:YES afterDelay:4];
    
    [self creatData];
    [self creatPicView];
   
    // Do any additional setup after loading the view.
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
    
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;


    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
 

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    [self creatHeadView];
}
// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}

- (void)creatHeadView {
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 300)];
    self.headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = self.headView;
    //    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.right.top.equalTo(self.tableView);
    //        make.height.equalTo(@(300 * H));
    //    }];
    [self creatPicView];
    [self creatLable];
}
- (void)creatPicView {
    
    self.picView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*200) imageURLStringsGroup:self.picArr];
    [self.headView addSubview:self.picView];
    self.mapButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.mapButton setBackgroundImage:[UIImage imageNamed:@"iconfont-ditu"] forState:UIControlStateNormal];
    self.mapButton.frame = CGRectMake(WIDTH*320, HEIGHT*20, WIDTH*30, HEIGHT*30);
    [self.picView addSubview:self.mapButton];
    [self.mapButton addTarget:self action:@selector(mapClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)creatLable {
    
    self.topLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * WIDTH, HEIGHT*200, WIDTH*355, HEIGHT*60)];
    self.topLabel.textColor = [UIColor colorWithWhite:0.380 alpha:1.000];
    self.topLabel.font = [UIFont systemFontOfSize:13];
    self.topLabel.numberOfLines = 0;
    self.cnLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, HEIGHT*140, WIDTH*375, HEIGHT*20)];
    self.cnLabel.textColor = [UIColor whiteColor];
    self.cnLabel.font = [UIFont boldSystemFontOfSize:15];
    self.enLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, HEIGHT*160, WIDTH*375, HEIGHT*20)];
    self.enLabel.textColor = [UIColor whiteColor];
    self.enLabel.font = [UIFont boldSystemFontOfSize:15];
    [self.headView addSubview:self.topLabel];
    [self.headView addSubview:self.cnLabel];
    [self.headView addSubview:self.enLabel];
     [self creatButton];
    

}
- (void)mapClick:(UIButton *)button {
    MapViewController *mapVC = [[MapViewController alloc]init];
    mapVC.countryId = self.countryId;
    [self.navigationController pushViewController:mapVC animated:YES];
}
- (void)creatButton {
    self.lineImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, HEIGHT*295, WIDTH*375, HEIGHT*1)];
    [self.lineImage setImage:[UIImage imageNamed:@"best_line"]];
    [self.headView addSubview:self.lineImage];
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.rightButton.frame = CGRectMake(WIDTH*270, HEIGHT*270, WIDTH*100, HEIGHT*10);
    [self.rightButton setTitle:@"国家实用信息 >" forState:UIControlStateNormal];
    self.rightButton.tintColor = [UIColor colorWithRed:0.071 green:0.600 blue:0.049 alpha:1.000];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.rightButton addTarget:self action:@selector(pushButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:self.rightButton];
    
 
    
}

- (void)createMJRefresh {
    // 上拉加载
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(creatData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        return self.newsArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
            static NSString *reuse = @"cell";
        HotCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[HotCollectCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor clearColor];
        }
        cell.delegate = self;
        cell.hotArr = self.cityArr;
        [cell.collect reloadData];
        return cell;
    }else {
            static NSString *reuse = @"discell";
        DiscountCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[DiscountCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor clearColor];
        }
        CountryModel *dislist = self.newsArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:dislist.photo] placeholderImage:[UIImage imageNamed:@"F"]];
        cell.topLabel.text = dislist.title;
        cell.leftLabel.text = dislist.priceoff;
        NSString *str = [[dislist.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""]stringByReplacingOccurrencesOfString:@"</em>元起" withString:@""];
     
        cell.numberLabel.text = str ;
        cell.rightLabel.text = @"元起";
        return cell;

    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DisCountWebView *web = [[DisCountWebView alloc]init];
    CountryModel *dislist = self.newsArr[indexPath.row];
    web.disCountStr = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.6&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_user_id=6654242%%20",dislist.uId];
    [self.navigationController pushViewController:web animated:YES];
    
}
// 设置分区高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 230 * HEIGHT;
    }
    
       return HEIGHT * 100;
}
//设置分区头高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (self.newsArr.count == 0 && section == 1) {
        return 0;
    }
        return HEIGHT * 50;
}
//设置分区头内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT *-20, WIDTH * 375, HEIGHT * 50)];
    view.backgroundColor = [UIColor whiteColor];
    view.alpha = 0.7;
    
    self.sectLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 10, HEIGHT * 20, WIDTH * 100, HEIGHT * 20)];
    [view addSubview:self.sectLabel];
    if (section == 0) {
        self.sectLabel.text = @"热门城市";
    }else if (section == 1&&self.newsArr.count!=0) {
        self.sectLabel.text = @"超值自由行";
    }
    if (self.cityArr.count>=4&&section == 0) {
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
        rightButton.tag = 1000;
        rightButton.frame = CGRectMake(WIDTH * 270, HEIGHT * 20, WIDTH * 100, HEIGHT * 20);
        [rightButton setTitle:@"查看全部" forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
        rightButton.tintColor = [UIColor colorWithRed:0.071 green:0.600 blue:0.049 alpha:1.000];
        [view addSubview:rightButton];


    }else if (self.newsArr.count>=5&&section ==1) {
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
        rightButton.tag = 1001;
        rightButton.frame = CGRectMake(WIDTH * 270, HEIGHT * 20, WIDTH * 100, HEIGHT * 20);
        [rightButton setTitle:@"查看全部" forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
        rightButton.tintColor = [UIColor colorWithRed:0.071 green:0.600 blue:0.049 alpha:1.000];
        [view addSubview:rightButton];
    }
    return view;
}
- (void)rightClick:(UIButton *)button {
    //跳转查看全部页面,通过tag判断
    if (button.tag == 1000) {
        AllHotCountryViewController *allHotCou = [[AllHotCountryViewController alloc]init];
        allHotCou.allCity = self.countryId;
        
        allHotCou.placeName = self.countryDic[@"cnname"];
        [self.navigationController pushViewController:allHotCou animated:YES];
    }else if (button.tag == 1001) {
        AllDiscountViewController *allDisV = [[AllDiscountViewController alloc]init];
        //1对应国家,
        allDisV.type = @"1";
        allDisV.allDiscount = self.countryId;
        allDisV.placeName = self.countryDic[@"cnname"];
        [self.navigationController pushViewController:allDisV animated:YES];
    }
    
}





- (void)pushButton:(UIButton *)button {
    SuperWebViewViewController *web = [[SuperWebViewViewController alloc]init];
    
    web.countryId = self.countryId;
    [self.navigationController pushViewController:web animated:YES];
    
}
- (void)send:(NSString *)cityNum {
    CityViewController *cVC = [[CityViewController alloc]init];
    cVC.cityId = cityNum;
    [self.navigationController pushViewController:cVC animated:YES];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.topLabel.textColor = [UIColor whiteColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.topLabel.textColor = [UIColor colorWithWhite:0.380 alpha:1.000];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

    
@end
